# -*- coding: utf-8 -*-
'''
Created on 12.08.2019

@author: yu03
'''
# -*- coding: utf-8 -*-
'''
Created on 01.08.2019

@author: yu03
'''

from PyUeye import file_name
from PyUeye.IDS_lib.pyueye_camera import Camera
from PyUeye.IDS_lib.pyueye_utils import FrameThread
from PyUeye.IDS_lib.pyueye_gui import PyuEyeQtApp, PyuEyeQtView
from PyQt5 import QtGui
from pyueye import ueye
import cv2
import numpy as np
import time
from PyQt5.QtWidgets import QApplication
import datetime

now = datetime.datetime.now()
num = 0

def process_image(self, image_data):
    # reshape the image data as 1dimensional array
    time_stamp = time.time()
    global num, f
    num += 1
    image = image_data.as_1d_image()
#     print(image[0])
    line = np.array([image[:], time_stamp])
    
    # show the image with Qt
    return QtGui.QImage(image.data,
                        image_data.mem_info.width,
                        image_data.mem_info.height,
                        QtGui.QImage.Format_Grayscale8)
 
def main():
    
#     AOI = [0, 405, 1280, 16]
    AOI = [0, 0, 1280, 16]
    Exposure = 0.07
    Trigger_External = 1
    Blacklevel = 0, 200
#     Data_fomate = 'SENSOR_RAW10'
    Data_fomate = 'MONO8'
    
    
    # we need a QApplication, that runs our QT Gui Framework    
    app = PyuEyeQtApp()
  
    # a basic qt window
    view = PyuEyeQtView()
    view.show()
    view.user_callback = process_image
 
    # camera class to simplify uEye API access
    cam = Camera()
    cam.init()
    
    if Data_fomate == 'MONO8':
        cam.set_colormode(ueye.IS_CM_MONO8)
    elif Data_fomate == 'SENSOR_RAW10':
        cam.set_colormode(ueye.IS_CM_SENSOR_RAW10)
    
    if cam.get_colormode() == 33:
        print('Color Mode: Sensor RAW 10')
    elif cam.get_colormode() == 6:
        print('Color Mode: MONO 8')
#     print(ueye.IS_CM_BGR8_PACKED)

    if Blacklevel[0] == 0:
        cam.set_blacklevel_mode(ueye.IS_AUTO_BLACKLEVEL_OFF)
        cam.set_blacklevel(Blacklevel[1])
        print('Blacklevel Mode: Manual', cam.get_blacklevel_mode(), 'Blacklevel:', cam.get_blacklevel())

    cam.set_aoi(AOI[0], AOI[1], AOI[2], AOI[3])
    aoi = cam.get_aoi()
    print('AOI:', aoi.x, aoi.y, aoi.width, aoi.height)
    
    if Trigger_External == 1:
        cam.set_external_trigger()
    elif Trigger_External == 0:
        print('Frame Time Range: %f to %f (increment:%f) [S]'%(cam.get_FrameTimeRange()[0], cam.get_FrameTimeRange()[1],cam.get_FrameTimeRange()[2]))
        cam.set_fps(25)
#         cam.set_fps(1/cam.get_FrameTimeRange()[0])

    cam.set_exposure(Exposure)
    print('Exposure Time:', cam.get_exposure())
    
    cam.turn_on_flash_high()
    print(cam.get_flash_mode())
    
    flash_params_min = cam.get_flash_range()[0]
    flash_params_max = cam.get_flash_range()[1]
    flash_params_inc = cam.get_flash_range()[2]
    print('Flash Delay Range: %i to %i (increment:%i) [us]'%(flash_params_min.s32Delay, flash_params_max.s32Delay, flash_params_inc.s32Delay))
    print('Flash Duration: %i to %i (increment:%i) [us]'%(flash_params_min.u32Duration, flash_params_max.u32Duration, flash_params_inc.u32Duration))
    
    
    if cam.get_flash_mode() == 0:
        print('Flash: OFF')

    cam.set_flash_params(-30,500)
    flash_params = cam.get_flash_params()
    print('Flash: %i[us] Delay; %i[us] Duration'%(flash_params.s32Delay, flash_params.u32Duration))
    
#     print(flash_params.s32Delay)
#     print(flash_params.u32Duration)
#     print(cam.get_colordepth()[0], cam.get_colordepth()[1])
    cam.alloc()
    cam.capture_video()
#     cam.freeze_video()
     
    #a thread that waits for new images and processes all connected views
    thread = FrameThread(cam, view)
#     thread.setDaemon(True)
    thread.start()
    
    # cleanup
    app.exit_connect(thread.stop)
    app.exec_()
  
    print('Frame Rate:', cam.get_fps())
    thread.stop()
    thread.join()
 
    cam.stop_video()
    cam.exit()
 
if __name__ == "__main__":
    main()